package com.print;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.PrintException;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.swetake.util.Qrcode;

public class Main {

	/**
	 * @param args
	 * @throws PrintException
	 * @throws ImageFormatException
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {
		Qrcode rcode = new Qrcode();
		rcode.setQrcodeVersion(3);
		byte[] content = "http://hao123.com11111111254654".getBytes("utf-8");
		BufferedImage bufImg = new BufferedImage(300, 300,
					        BufferedImage.TYPE_INT_RGB);
		
		Graphics2D gs = bufImg.createGraphics(); 
		gs.setBackground(Color.WHITE);
		gs.clearRect(0, 0, 300, 300); 
		gs.setColor(Color.BLACK); 
		if (content.length > 0 && content.length < 120) {
            boolean[][] codeOut = rcode.calQrcode(content);
            for (int i = 0; i < codeOut.length; i++) {
                for (int j = 0; j < codeOut.length; j++) {
                    if (codeOut[j][i]) {
                        gs.fillRect(j * 10 + 5, i * 10 + 5, 10, 10);
                    }
                }
            }
            System.out.println("OK");
        } else {
            System.err.println("QRCode content bytes length = " + content.length + " not in [ 0,120 ]. ");
        }
        gs.dispose();
        bufImg.flush();
        File imgFile = new File("E:\\test.png");
        try {
            ImageIO.write(bufImg, "png", imgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

	public void printIvo(String ivoLogo, String ivoModel, String sn,
			String csn, String r, String hw, String fw, String zb, String sd,
			boolean printRu) throws Exception {
		Label label = new Label();
		label.addText(new Text(2, 6, "Arial Black", 1, 21.5, ivoLogo));
		label.addText(new Text(20, 5.2, "Arial", 0, 15.5, ivoModel));
		label.addCode(new Code(2.8, 7.6, 5.4, 0.19, sn));
		label.addText(new Text(2.8, 16.4, "Arial", 0, 9.5, sn));
		label.addCode(new Code(2.8, 18.4, 5.4, 0.174, csn));
		label.addText(new Text(2.8, 27.4, "Arial", 0, 9.5, csn));
		if (printRu) {
			label.addText(new Text(51.8, 8.2, "RU Type", 0, 24.5, "U"));
			label.addText(new Text(53.8, 11.2, "Helvetica-Condensed-Black-Se",
					0, 12, "E 309667"));
		}
		label.addText(new Text(55.8, 14.6, "Arial Narrow", 0, 12, "RoHS"));
		label.addText(new Text(55.8, 17.4, "Arial", 0, 9.5, r));
		label.addText(new Text(55.8, 20.2, "Arial", 0, 9.5, hw));
		label.addText(new Text(55.8, 23.4, "Arial", 0, 9.5, fw));
		label.addText(new Text(55.8, 26.4, "Arial", 0, 9.5, zb));
		label.addText(new Text(2.2, 30.2, "Arial", 0, 9.5, sd));
		label.addText(new Text(46.6, 30.2, "Arial", 0, 9.5, "MADE IN CHINA"));
		label.print();
		label.dispose();
	}

	public void printLenovo(String ivoModel, String sn, String csn, String r,
			String hw, String fw, String zb, String sd, String pn, String fru,
			String date, boolean printRu) throws Exception {
		Label label = new Label();
		label.addText(new Text(2, 6, "Arial Black", 1, 21.5, "IVO"));
		label.addText(new Text(20, 5.2, "Arial", 1, 15.5, ivoModel));
		label.addCode(new Code(2.8, 7.6, 5.4, 0.178, sn));
		label.addText(new Text(2.8, 16.4, "Arial", 0, 9.5, sn));
		label.addCode(new Code(2.8, 18.4, 5.4, 0.176, csn));
		label.addText(new Text(2.8, 27.4, "Arial", 0, 9.5, csn));
		if (printRu) {
			label.addText(new Text(51.8, 8.2, "RU Type", 0, 24.5, "U"));
			label.addText(new Text(53.8, 11.2, "Helvetica-Condensed-Black-Se",
					0, 12, "E 309667"));
		}
		label.addText(new Text(55.8, 14.6, "Arial Narrow", 0, 12, "RoHS"));
		label.addText(new Text(55.8, 17.4, "Arial", 0, 9.5, r));
		label.addText(new Text(55.8, 20.2, "Arial", 0, 9.5, hw));
		label.addText(new Text(55.8, 23.4, "Arial", 0, 9.5, fw));
		label.addText(new Text(55.8, 26.4, "Arial", 0, 9.5, zb));
		label.addText(new Text(2.2, 30.2, "Arial", 0, 9.5, sd));
		label.addText(new Text(46.6, 30.2, "Arial", 0, 9.5, "MADE IN CHINA"));
		label.addText(new Text(5.8, 30.2, "Arial", 0, 6.5, pn));
		label.addText(new Text(21.5, 30.2, "Arial", 0, 6.5, fru));
		label.addText(new Text(37.5, 30.2, "Arial", 0, 6.5, date));
		label.print();
		label.dispose();
	}

	public void printSamsung(String ivoModel, String sn, String csn, String r,
			String hw, String fw, String zb, String samsungPartNo, String week,
			boolean printRu) throws Exception {
		Label label = new Label();
		label.addText(new Text(2, 6, "Arial Black", 1, 21.5, "IVO"));
		label.addText(new Text(20, 5.2, "Arial", 1, 15.5, ivoModel));
		label.addCode(new Code(2.8, 7.6, 5.4, 0.19, sn));
		label.addText(new Text(2.8, 16.4, "Arial", 0, 9.5, sn));
		label.addCode(new Code(2.8, 18.4, 5.4, 0.173, csn));
		label.addText(new Text(2.8, 27.4, "Arial", 0, 9.5, csn));
		if (printRu) {
			label.addText(new Text(51.8, 8.2, "RU Type", 0, 24.5, "U"));
			label.addText(new Text(53.8, 11.2, "Helvetica-Condensed-Black-Se",
					0, 12, "E 309667"));
		}
		label.addText(new Text(55.8, 14.6, "Arial Narrow", 0, 12, "RoHS"));
		label.addText(new Text(55.8, 17.4, "Arial", 0, 9.5, r));
		label.addText(new Text(55.8, 20.2, "Arial", 0, 9.5, hw));
		label.addText(new Text(55.8, 23.4, "Arial", 0, 9.5, fw));
		label.addText(new Text(55.8, 26.4, "Arial", 0, 9.5, zb));
		label.addText(new Text(46.6, 30.2, "Arial", 0, 9.5, "MADE IN CHINA"));
		label.addText(new Text(9.3, 30, "Arial", 0, 6.5, samsungPartNo));
		label.addText(new Text(32.5, 30, "Arial", 0, 6.5, week));
		label.print();
		label.dispose();
	}

	public void printSharp(String sharpPartNo, String sn, String csn, String r,
			String hw, String fw, String sd) throws Exception {
		Label label = new Label();
		label.addText(new Text(2, 6, "Sharp Type", 1, 21, "S"));
		label.addText(new Text(22, 5.2, "Arial Unicode MS", 1, 14.5,
				sharpPartNo));
		label.addCode(new Code(3, 8.2, 4.3, 0.2, sn));
		label.addText(new Text(3, 15.1, "Arial Unicode MS", 0, 11, sn));
		label.addCode(new Code(3, 16.5, 4.3, 0.18, csn));
		label.addText(new Text(3, 23.5, "Arial Unicode MS", 0, 11, csn));
		label.addText(new Text(56.3, 6.4, "Arial Narrow", 0, 17, "RoHS"));
		label.addText(new Text(56.3, 9.8, "Arial Unicode MS", 0, 11, r));
		label.addText(new Text(56.3, 13.8, "Arial Unicode MS", 0, 11, hw));
		label.addText(new Text(56.3, 17.8, "Arial Unicode MS", 0, 11, fw));
		label.addText(new Text(2, 26.2, "Arial Unicode MS", 0, 11, sd));
		label.addText(new Text(47.5, 26.2, "Arial Unicode MS", 0, 11,
				"MADE IN CHINA"));
		label.print();
		label.dispose();
	}

	public void print1907(String ivoModel, String sn, String csn, String r,
			String hw, String fw, String sd, String zb, boolean printRu)
			throws Exception {
		Label label = new Label();
		label.addText(new Text(50, 5, "Arial", 0, 11, "IVO"));
		label.addCode(new Code(2, 2, 3.1, 0.19, sn));
		String space = " ";
		label.addText(new Text(2, 7, "Arial", 0, 6.8, sn + space + ivoModel
				+ space + r + space + hw + space + fw));
		label.addCode(new Code(2, 8, 3.1, 0.16, csn));
		space = "    ";
		label.addText(new Text(2, 13, "Arial", 0, 6.8, csn + space + "RoHS"
				+ space + sd + space + zb + space + "MADE IN CHINA"));
		if (printRu) {
			label.addText(new Text(48.8, 9.2, "RU Type", 0, 9.5, "U"));
			label.addText(new Text(48.3, 11, "Arial", 1, 6.5, "E 309667"));
		}
		label.print();
		label.dispose();
	}

	public void printSharpSmall(String ivoModel, String sharpPartNo, String sn,
			String r, String hw, String fw, String zb, String sd)
			throws Exception {
		Label label = new Label();
		label.addCode(new Code(2, 2, 3.5, 0.22, sn));
		String space = "  ";
		label.addText(new Text(2, 7.5, "Arial", 0, 6.8, ivoModel + space + r
				+ space + hw + space + fw + space + sn));
		label.addText(new Text(2, 10, "Arial Unicode MS", 1, 10, sharpPartNo));
		space = "               ";
		label.addText(new Text(2, 12.7, "Arial", 0, 6.9, "RoHS" + space + sd
				+ space + zb + space + "MADE IN CHINA"));
		label.addText(new Text(43.4, 10.5, "Sharp Type", 0, 15, "S"));
		label.print();
		label.dispose();
	}

	public void printAuoSmall(String sn, String day, String auoModelNo)
			throws Exception {
		Label label = new Label();
		label.addCode(new Code(3, 3, 4.2, 0.2, sn));
		label.addText(new Text(3, 9.1, "Arial", 0, 6.9, sn));
		label
				.addText(new Text(3, 11.3, "Arial", 0, 6.9, "Manufactured  "
						+ day));
		label.addText(new Text(3, 13.9, "Arial", 0, 6.9, "Model No:"
				+ auoModelNo));
		label.addText(new Text(27.3, 11.3, "Arial", 0, 6.9, "AU Optronics"));
		label.addText(new Text(27.3, 13.9, "Arial", 0, 6.9, "MADE IN CHINA"));
		label.addRectangle(new Rectangle(43.4, 9.7, 6.3, 4.3, 0.2));
		label.addText(new Text(44.1, 12.4, "Arial", 1, 6.9, "RoHS"));
		label.addCircle(new Circle(53.5, 11.9, 2.7, 0.3));
		label.addLine(new Line(53.5, 11.9, 2.5, 0.2, -60));
		label.addLine(new Line(53.5, 11.9, 2.5, 0.2, 120));
		label.addText(new Text(51.9, 12.9, "Arial", 1, 10, "Pb"));
		label.print();
		label.dispose();
	}

	public void printPanasonic(String ivoModel, String panasonicModel,
			String sn, String csn, String r, String hw, String fw, String zb,
			String sd) throws Exception {
		Label label = new Label();
		label.addText(new Text(2, 9.1, "Arial", 0, 6.8, hw));
		label.addText(new Text(2, 11.2, "Arial", 0, 6.8, "RoHS " + sd + "  "
				+ zb));
		label.addCode(new Code(31.5, 2, 3.8, 0.17, csn));
		label.addText(new Text(28.2, 7.5, "Arial", 0, 6.8, panasonicModel
				+ "    " + csn));
		label.addCode(new Code(13, 8, 3.8, 0.18, sn));
		label.addText(new Text(1.5, 13.5, "Arial", 0, 6.8, ivoModel + "  " + r
				+ "  " + fw + "  " + sn + "      MADE IN CHINA"));
		label.print();
		label.dispose();
	}

	public void printIvoPallet(String pfcd, String destShop, String count,
			String palletId) throws Exception {
		Label label = new Label();
		label.addText(new Text(3, 6, "PMingLiU", 0, 14, "料号"));
		label.addText(new Text(3, 12.3, "PMingLiU", 0, 14, "储位"));
		label.addText(new Text(3, 18.6, "PMingLiU", 0, 14, "數量"));
		label.addText(new Text(3, 25, "PMingLiU", 0, 14, "栈板号"));
		label.addText(new Text(14.8, 6, "Arial", 0, 14, "(Material)"));
		label.addText(new Text(14.8, 12.3, "Arial", 0, 14, "(Position)"));
		label.addText(new Text(14.8, 18.6, "Arial", 0, 14, "(Count)"));
		label.addText(new Text(14.8, 25, "Arial", 0, 14, "(Pallet ID)"));
		label.addText(new Text(35, 6, "Arial", 0, 14, pfcd.substring(0, 8)));
		label.addText(new Text(35, 12.3, "Arial", 0, 14, destShop + "  "
				+ pfcd.substring(8)));
		label.addText(new Text(35, 18.6, "Arial", 0, 14, count));
		label.addText(new Text(35, 25, "Arial", 0, 14, palletId));
		label.addCode(new Code(9, 29, 5.2, 0.35, palletId));
		label.addCode(new Code(51, 15.6, 4.2, 0.3, count));
		label.print();
		label.dispose();
	}

	public void printSamsungPallet(String pfcd, String destShop, String count,
			String palletId, String samsungPartNo) throws Exception {
		Label label = new Label();
		label.addText(new Text(3, 6, "PMingLiU", 0, 14, "料号"));
		label.addText(new Text(3, 11.3, "PMingLiU", 0, 14, "储位"));
		label.addText(new Text(3, 16.6, "PMingLiU", 0, 14, "數量"));
		label.addText(new Text(3, 22.5, "PMingLiU", 0, 14, "栈板号"));
		label.addText(new Text(3, 28.5, "PMingLiU", 0, 14, "客户码"));
		label.addText(new Text(14.8, 6, "Arial", 0, 14, "(Material)"));
		label.addText(new Text(14.8, 11.3, "Arial", 0, 14, "(Position)"));
		label.addText(new Text(14.8, 16.6, "Arial", 0, 14, "(Count)"));
		label.addText(new Text(14.8, 22.5, "Arial", 0, 14, "(Pallet ID)"));
		label.addText(new Text(14.8, 28.5, "Arial", 0, 14, "(Part No)"));
		label.addText(new Text(35, 6, "Arial", 0, 14, pfcd.substring(0, 8)));
		label.addText(new Text(35, 11.3, "Arial", 0, 14, destShop + "  "
				+ pfcd.substring(8)));
		label.addText(new Text(35, 16.6, "Arial", 0, 14, count));
		label.addText(new Text(35, 22.5, "Arial", 0, 14, palletId));
		label.addText(new Text(35, 28.5, "Arial", 0, 14, samsungPartNo));
		label.addCode(new Code(9, 30.9, 5.3, 0.36, palletId));
		label.print();
		label.dispose();
	}

	public void printMergeCrr(String crrId, String weight, String count)
			throws Exception {
		Label label = new Label();
		label.addRectangle(new Rectangle(2, 1.8, 83, 40.1, 0.28));
		label.addText(new Text(8, 7.8, "宋体", 1, 16, "并箱箱号："));
		label.addText(new Text(8, 15.6, "宋体", 1, 16, "并箱重量："));
		label.addText(new Text(8, 23.3, "宋体", 1, 16, "并箱数量："));
		label.addText(new Text(32, 7.8, "Arial", 0, 14, crrId));
		label.addText(new Text(32, 15.6, "Arial", 0, 14, weight));
		label
				.addText(new Text(32, 23.3, "Arial", 0, 14, count
						+ "  PCS/Carton"));
		label.addCode(new Code(10, 26.5, 8.7, 0.33, crrId));
		label.print();
		label.dispose();
	}

	// 同松下
	public void printIvoCarton(String model, String rev, String qty,
			String weight, String cartonId, String remark, String zb)
			throws Exception {
		Label label = new Label();
		label.addText(new Text(3, 6, "PMingLiU", 0, 14, "型號"));
		label.addText(new Text(3, 11, "PMingLiU", 0, 14, "版本"));
		label.addText(new Text(3, 16, "PMingLiU", 0, 14, "數量"));
		label.addText(new Text(3, 21, "PMingLiU", 0, 14, "重量"));
		label.addText(new Text(3, 26, "PMingLiU", 0, 14, "箱號"));
		label.addText(new Text(10.5, 6, "Arial", 0, 14, "(Module)"));
		label.addText(new Text(10.5, 11, "Arial", 0, 14, "(Rev.)"));
		label.addText(new Text(10.5, 16, "Arial", 0, 14, "(QTY)"));
		label.addText(new Text(10.5, 21, "Arial", 0, 14, "(Weight)"));
		label.addText(new Text(10.5, 26, "Arial", 0, 14, "(Carton ID)"));
		label.addText(new Text(36, 6, "Arial", 0, 14, model));
		label.addText(new Text(36, 11, "Arial", 0, 14, rev));
		label.addText(new Text(36, 16, "Arial", 0, 14, qty));
		label.addText(new Text(36, 21, "Arial", 0, 14, weight));
		label.addText(new Text(36, 26, "Arial", 0, 14, cartonId));
		label.addCode(new Code(12, 28, 5.2, 0.26, cartonId));
		label.addText(new Text(3, 37, "PMingLiU", 0, 14, "備註"));
		label.addText(new Text(10.5, 37, "Arial", 0, 14, "(Remark)"));
		label.addText(new Text(36, 37, "Arial", 0, 14, remark));
		label.addText(new Text(61.5, 38, "Arial", 0, 14, zb));
		label.print();
		label.dispose();
	}

	public void printSamsungCarton(String model, String rev, String qty,
			String weight, String cartonId, String remark, String zb,
			String samsungPartNo, String sflg) throws Exception {
		Label label = new Label();
		label.addText(new Text(3, 6, "PMingLiU", 0, 14, "型號"));
		label.addText(new Text(3, 10.5, "PMingLiU", 0, 14, "版本"));
		label.addText(new Text(3, 15, "PMingLiU", 0, 14, "數量"));
		label.addText(new Text(3, 19.5, "PMingLiU", 0, 14, "重量"));
		label.addText(new Text(3, 24, "PMingLiU", 0, 14, "箱號"));
		label.addText(new Text(10.5, 6, "Arial", 0, 14, "(Module)"));
		label.addText(new Text(10.5, 10.5, "Arial", 0, 14, "(Rev.)"));
		label.addText(new Text(10.5, 15, "Arial", 0, 14, "(QTY)"));
		label.addText(new Text(10.5, 19.5, "Arial", 0, 14, "(Weight)"));
		label.addText(new Text(10.5, 24, "Arial", 0, 14, "(Carton ID)"));
		label.addText(new Text(3, 27.5, "PMingLiU", 0, 14, "Part No"));
		label.addText(new Text(36, 6, "Arial", 0, 14, model));
		label.addText(new Text(36, 10.5, "Arial", 0, 14, rev));
		label.addText(new Text(36, 15, "Arial", 0, 14, qty));
		label.addText(new Text(36, 19.5, "Arial", 0, 14, weight));
		label.addText(new Text(36, 24, "Arial", 0, 14, cartonId));
		label.addText(new Text(36, 27.5, "Arial", 0, 14, samsungPartNo));
		label.addCode(new Code(12, 29, 5.2, 0.26, cartonId));
		label.addText(new Text(3, 38, "PMingLiU", 0, 14, "備註"));
		label.addText(new Text(10.5, 38, "Arial", 0, 14, "(Remark)"));
		label.addText(new Text(36, 38, "Arial", 0, 14, remark));
		label.addText(new Text(61.5, 39, "Arial", 0, 14, zb));
		label.addText(new Text(69, 38, "Arial", 0, 14, sflg));
		label.print();
		label.dispose();
	}

	public void printLenovoCarton(String model, String rev, String qty,
			String weight, String cartonId, String remark, String zb,
			String pn, String fru, String hc, String date) throws Exception {
		Label label = new Label();
		label.addText(new Text(3, 6, "PMingLiU", 0, 14, "型號"));
		label.addText(new Text(3, 11, "PMingLiU", 0, 14, "版本"));
		label.addText(new Text(3, 16, "PMingLiU", 0, 14, "數量"));
		label.addText(new Text(3, 21, "PMingLiU", 0, 14, "重量"));
		label.addText(new Text(3, 26, "PMingLiU", 0, 14, "箱號"));
		label.addText(new Text(10.5, 6, "Arial", 0, 14, "(Module)"));
		label.addText(new Text(10.5, 11, "Arial", 0, 14, "(Rev.)"));
		label.addText(new Text(10.5, 16, "Arial", 0, 14, "(QTY)"));
		label.addText(new Text(10.5, 21, "Arial", 0, 14, "(Weight)"));
		label.addText(new Text(10.5, 26, "Arial", 0, 14, "(Carton ID)"));
		label.addText(new Text(36, 6, "Arial", 0, 14, model));
		label.addText(new Text(36, 11, "Arial", 0, 14, rev));
		label.addText(new Text(36, 16, "Arial", 0, 14, qty));
		label.addText(new Text(36, 21, "Arial", 0, 14, weight));
		label.addText(new Text(36, 26, "Arial", 0, 14, cartonId));
		label.addCode(new Code(12, 28, 5.2, 0.26, cartonId));
		label.addText(new Text(3, 37, "PMingLiU", 0, 14, "備註"));
		label.addText(new Text(10.5, 37, "Arial", 0, 14, "(Remark)"));
		label.addText(new Text(36, 37, "Arial", 0, 14, remark));
		label.addText(new Text(61.5, 38, "Arial", 0, 14, zb));
		label.addText(new Text(3, 41.5, "Arial", 0, 11, pn));
		label.addText(new Text(22, 41.5, "Arial", 0, 11, fru));
		label.addText(new Text(44, 41.5, "Arial", 0, 11, hc));
		label.addText(new Text(62.5, 41.5, "Arial", 0, 11, date));
		label.print();
		label.dispose();
	}

	public void printSharpCarton(String model, String lotNo, String qty,
			String cartonId) throws Exception {
		Label label = new Label();
		label.addText(new Text(3, 6, "宋体", 0, 13, "社内品番："));
		label.addText(new Text(33, 6, "Arial Narrow", 1, 17, "(4S)" + model));
		label.addCode(new Code(8, 7.2, 3.6, 0.28, "4S" + model));
		label.addText(new Text(3, 14.5, "Arial", 0, 15,
				"L o t  N o.          :"));
		label.addText(new Text(43, 14.5, "Arial Narrow", 0, 15, "(1T)  "
				+ lotNo + "  VE"));
		label.addCode(new Code(8, 16, 3.6, 0.28, "1T"
				+ lotNo.replaceAll("\\.", "") + "  VE"));
		label.addText(new Text(3, 23, "Arial", 0, 15, "Q u a n t i t y :"));
		label.addText(new Text(43, 23, "Arial Narrow", 0, 15, "(Q)  " + qty
				+ "               PCS"));
		label.addCode(new Code(8, 24.5, 3.6, 0.28, "Q" + qty));
		label.addText(new Text(3, 31, "Arial", 0, 15, "c a r t o n :"));
		label.addText(new Text(33, 31, "Arial", 0, 15, cartonId));
		label.addCode(new Code(8, 32, 3.6, 0.28, cartonId));
		label.addText(new Text(3, 38.5, "宋体", 0, 9.5, "シャープ物流用ラベルです。"));
		label.print();
		label.dispose();
	}

	public void printPVO(String pvoModel, String sn, String csn, String hw,
			String fw, String sd) throws Exception {
		Label label = new Label();
		label.addText(new Text(48.5, 5, "Arial", 0, 11, "PVO"));
		label.addCode(new Code(2.1, 2, 3.1, 0.19, sn));
		label.addText(new Text(2, 7, "Arial", 0, 7.1, sn + " " + pvoModel + " "
				+ hw + " " + fw));
		label.addCode(new Code(2, 8, 3.3, 0.16, csn));
		label.addText(new Text(2, 13, "Arial", 0, 7.1, csn + "   " + "RoHS"
				+ "   " + sd + "     " + "MADE IN CHINA"));
		label.print();
		label.dispose();
	}
}
